package cn.wuqingna.skillview

import cn.wuqingna.skillview.SkillView.reloadSkillViewPapi
import com.bh.planners.api.Counting
import com.bh.planners.api.PlannersAPI
import com.bh.planners.api.PlannersAPI.plannersProfile
import com.bh.planners.api.*
import com.bh.planners.api.Counting.map
import com.bh.planners.api.PlannersAPI.jobs
import com.bh.planners.core.*
import com.bh.planners.core.storage.Storage
import eos.moe.dragoncore.api.event.KeyPressEvent
import eos.moe.dragoncore.config.Config
import eos.moe.dragoncore.network.PacketSender
import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerJoinEvent
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.Plugin
import taboolib.common.platform.event.SubscribeEvent
import taboolib.module.configuration.Configuration
import taboolib.module.kether.*


object SkillView : Plugin() {
    @taboolib.module.configuration.Config("skill.yml")
    lateinit var skill: Configuration
    @taboolib.module.configuration.Config("config.yml")
    lateinit var config: Configuration
    override fun onDisable() {
        super.onDisable()
    }


    override fun onEnable() {
        super.onEnable()

    }

    @SubscribeEvent
    fun join(event: PlayerJoinEvent){
        event.player.reloadSkillViewPapi()
    }

    /**
     * 监听按键
     */
    @SubscribeEvent
    fun key(event: KeyPressEvent) {
        PlannersAPI.keySlots.forEach {
            if (event.key == it.name){
                val skill = event.player.plannersProfile.getSkill(it)
                if (skill != null) {
                    PlannersAPI.cast(event.player, skill).handler(event.player, skill.instance)
                }
            }
        }
    }

    /**
     * 注册按键
     */
    @Awake(LifeCycle.ENABLE)
    fun registerKey() {
        PlannersAPI.keySlots.forEach {
            Config.registeredKeys.add(it.name)
        }
    }


    fun Player.reloadjobPapi() {
        val mapa = mutableMapOf<String, String>()
        val plannersProfile =  this.plannersProfile
        val skills = plannersProfile.getSkills()

        jobs.forEach {
            mapa["skill-job-name"] = it.option.name //职业昵称
        }

        PlannersAPI.profiles.forEach{
            mapa["skill-job-point"] = it.value.point.toString() //技能点
            mapa["skill-job-level"] = it.value.level.toString() //职业等级
            mapa["skill-job-exp"] = it.value.experience.toString() //当前经验
            mapa["skill-job-maxexp"] = it.value.maxExperience.toString() //最大经验
        }
        PacketSender.sendDeletePlaceholderCache(this, "skill-job", true)
        PacketSender.sendSyncPlaceholder(this, mapa)
    }

    fun Player.reloadSkillViewPapi(){

        val map = mutableMapOf<String, String>()
        val plannersProfile =  this.plannersProfile
        val skills = plannersProfile.getSkills()
        map["skill-view-amount"] = skills.size.toString() //职业技能数量


        PlannersAPI.keySlots.forEach {
            if (plannersProfile.getSkill(it) != null)
            if (skill.contains(plannersProfile.getSkill(it)!!.name)){
                map["skill-view-${it.name}"] = skill.getString(plannersProfile.getSkill(it)!!.name)!!
            }
        }
        var amount = 0;
        skills.forEach {
            if (skill.contains(it.name)){
                map["skill-view-$amount-img"] = skill.getString(it.name)!!
            }
            map["skill-view-$amount-name"] = it.name
            map["skill-view-$amount-key"] = it.key
            map["skill-view-$amount-level"] = it.level.toString()
            map["skill-view-$amount-cooldown"] = (Counting.getCountdown(this, it.instance) / 1000).toString() //技能冷却
            amount++
        }
        PacketSender.sendDeletePlaceholderCache(this, "skill-view", true)
        PacketSender.sendSyncPlaceholder(this, map)
    }

}